package co.fitstart.mobile.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.entity.coach.CoachRestCalendar;
import co.fitstart.entity.coach.CoachRestType;
import co.fitstart.entity.principal.Principal;
import co.fitstart.mobile.service.CoachRestCalendarService;

/**
 * 
 * @author wangliwang
 *
 */
@Service("coachRestCalendarService")
public class CoachRestCalendarServiceImpl implements CoachRestCalendarService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /* (non-Javadoc)
     * @see co.fitstart.mobile.service.CoachRestCalendarService#mapCoachRestCalendar(co.fitstart.entity.principal.Principal, java.util.Date)
     */
    @SuppressWarnings("unchecked")
    public Map<String, CoachRestCalendar> mapCoachRestCalendar(Principal coach, Date day, boolean experience) {
        
        String sentence = "FROM CoachRestCalendar WHERE coach = :coach AND restDate = :restDate AND coachRestType != :coachRestType AND disabled = false";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("coach", coach);
        parameters.put("restDate", day);
        parameters.put("coachRestType", CoachRestType.DUTY);
        
        List<CoachRestCalendar> coachRestCalendars = genericDaoSupport.searchForList(sentence, parameters);
        Map<String, CoachRestCalendar> coachRestMap = CollectionUtils.constructMapValueFromList(coachRestCalendars, "dayhour");
        
        Map<String, CoachRestCalendar> result = new HashMap<String, CoachRestCalendar>();
        
        result.putAll(coachRestMap);
        for (String key: coachRestMap.keySet()) {
            String hour = StringUtils.split(key, "|")[1];
            Date startTime = DateUtils.parseDate(DateFormatUtils.format(day, "yyyy-MM-dd ") + hour + ":00", "yyyy-MM-dd HH:mm:ss");
            
            Date beforeTime = org.apache.commons.lang3.time.DateUtils.addMinutes(startTime, -30);
            if(!result.containsKey(DateFormatUtils.format(beforeTime, "yyyy-MM-dd|HH:mm"))) {
                result.put(DateFormatUtils.format(beforeTime, "yyyy-MM-dd|HH:mm"), coachRestMap.get(key));
            }
            
            if(experience) {
                Date beforeAnhour = org.apache.commons.lang3.time.DateUtils.addHours(startTime, -1);
                if(!result.containsKey(DateFormatUtils.format(beforeAnhour, "yyyy-MM-dd|HH:mm"))) {
                    result.put(DateFormatUtils.format(beforeAnhour, "yyyy-MM-dd|HH:mm"), coachRestMap.get(key));
                }
            }
            
        }
        
        return result;
        
    }
    
}
